package com.zhibi.util.mail;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class MailUtil {

	private static String HOST;
	private static String USERNAME;
	private static String PASSWORD;
	private static int PORT;
	private static String CHARSET="utf-8";
	private static String FROM;

	public static void init(String host,int port,String username,String password){
		MailUtil.HOST = host;
		MailUtil.PORT = port;
		MailUtil.USERNAME = username;
		MailUtil.FROM = username;
		MailUtil.PASSWORD = password;
	}

	/**
	 * 发送普通邮件
	 * @Date 2016年1月14日 下午2:26:45
	 * @param subject
	 * @param msg
	 * @param to
	 * @throws EmailException
	 */
	public static void send(String subject,String msg,String to)
			throws EmailException{
		Email email = new SimpleEmail();
		email.setHostName(HOST);
		email.setSmtpPort(PORT);
		email.setAuthenticator(new DefaultAuthenticator(USERNAME, PASSWORD));
		email.setSSLOnConnect(true);
		email.setCharset(CHARSET);
		email.setFrom(FROM, "执笔");
		email.setSubject(subject);
		email.setMsg(msg);
		email.addTo(to);
		email.send();
	}


	/**
	 * 发送带附件的邮件
	 * @Date 2016年1月14日 下午2:51:13
	 * @param subject
	 * @param msg
	 * @param to
	 * @param file
	 * @param description  描述
	 * @throws EmailException
	 */
	public static void send(String subject,String msg,String to,File file,String... description) throws EmailException{
		EmailAttachment attachment = new EmailAttachment();
		attachment.setPath(file.getPath());
		attachment.setDisposition(EmailAttachment.ATTACHMENT);
		attachment.setDescription(description.length>0?description[0]:"");
		attachment.setName(file.getName());

		// Create the email message
		MultiPartEmail email = new MultiPartEmail();
		email.setHostName(HOST);
		email.setSmtpPort(PORT);
		email.setAuthenticator(new DefaultAuthenticator(USERNAME, PASSWORD));
		email.setSSLOnConnect(true);
		email.setCharset(CHARSET);
		email.setFrom(FROM, "执笔");

		email.addTo(to);
		email.setSubject(subject);
		email.setMsg(msg);
		email.attach(attachment);
		email.send();
	}

	/**
	 * 发送url邮件
	 * @Date 2016年1月14日 下午2:57:19
	 * @param subject
	 * @param msg
	 * @param to
	 * @param url
	 * @param description
	 * @throws EmailException
	 * @throws MalformedURLException
	 */
	public static void send(String subject,String msg,String to,String url,String description) throws EmailException, MalformedURLException{
		EmailAttachment attachment = new EmailAttachment();
		attachment.setURL(new URL(url));
		attachment.setDisposition(EmailAttachment.ATTACHMENT);
		attachment.setDescription(description);
		attachment.setName(url);

		// Create the email message
		MultiPartEmail email = new MultiPartEmail();
		email.setHostName(HOST);
		email.setSmtpPort(PORT);
		email.setAuthenticator(new DefaultAuthenticator(USERNAME, PASSWORD));
		email.setSSLOnConnect(true);
		email.setCharset(CHARSET);
		email.setFrom(FROM, "执笔");

		email.addTo(to);
		email.setSubject(subject);
		email.setMsg(msg);
		email.attach(attachment);
		email.send();
	}

	/**
	 * 发送html
	 * @Date 2016年1月14日 下午3:08:07
	 * @param subject
	 * @param msg
	 * @param to
	 * @param url
	 * @throws EmailException
	 * @throws MalformedURLException
	 */
	public static void send(String subject,String msg,String to,String url) throws EmailException, MalformedURLException{
		// Create the email message
		HtmlEmail email = new HtmlEmail();
		email.setHostName(HOST);
		email.setSmtpPort(PORT);
		email.setAuthenticator(new DefaultAuthenticator(USERNAME, PASSWORD));
		email.setSSLOnConnect(true);
		email.setCharset(CHARSET);
		email.setFrom(FROM, "执笔");
		
		// embed the image and get the content id
		String cid = email.embed(new URL(url), url);
		// set the html message
		email.setHtmlMsg("<html>"+cid+"</html>");
		// set the alternative message
		email.setTextMsg(msg);
		// send the email
		email.addTo(to);
		email.setSubject(subject);
		email.setMsg(msg);
		email.send();
	}

}
